/*
* $Id: EditItem.cpp 111 2010-07-01 15:26:44Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "EditItem.h"
#include "Control.h"
#include "Controls/EditDraw.h"
#include "Controls/CheckBoxDraw.h"
#include "Controls/DateDraw.h"

CEditItem::CEditItem( const std::tstring& strName, int nId, const std::tstring& strValue)
: CEditorItem(strName, nId), editItem(editItem::ANOTHER), m_strValueEdit(strValue), m_sizeHeader(0, 0), m_control(NULL){}
CEditItem::CEditItem(const std::tstring& strName, int nId, editItem::typeEdit type, const std::tstring& strValue)
: CEditorItem(strName, nId), editItem(type), m_strValueEdit(strValue), m_sizeHeader(0, 0), m_control(NULL){
	if(type == editItem::TEXT)
		m_control = new CEditDraw(m_strValueEdit, this);
	else if(type == editItem::INTEGER)
		m_control = new CNumberEditDraw(m_strValueEdit, this);
	else if(type == editItem::DOUBLE)
		m_control = new CDoubleEditDraw(m_strValueEdit, this);
	else if(type == editItem::BOOL)
		m_control = new CCheckBoxDraw(m_strValueEdit);
	else if(type == editItem::DATE)
		m_control = new CDateDraw(m_strValueEdit, this);
	else
		m_control = new CEditDraw(m_strValueEdit, this);
}
CEditItem::~CEditItem(){
	if(m_control)
		delete m_control;
}

void CEditItem::Paint(CDC* dc){
	m_control->SetPos(CPoint(m_size.cx/2 + 2, m_point.y));
	if(m_control)
		m_control->Paint(dc);
	CBrush brush;
	brush.CreateSolidBrush(CCommonFunction::GetBeeColor());
	dc->FillRect(CRect(m_point, CSize(m_size.cx/2, m_size.cy)), &brush);
	brush.DeleteObject();
	CPen pen;
	pen.CreatePen(PS_SOLID, 1, RGB(0,0,0));
	HGDIOBJ penOld = dc->SelectObject(pen);
	dc->MoveTo(m_point);
	dc->LineTo(CPoint(m_point.x + m_size.cx - 1, m_point.y));
	dc->LineTo(CPoint(m_point.x + m_size.cx - 1, m_point.y + m_size.cy));
	dc->LineTo(CPoint(m_point.x, m_point.y + m_size.cy));
	dc->LineTo(m_point);
	dc->MoveTo(CPoint(m_point.x + m_size.cx/2, m_point.y));
	dc->LineTo(CPoint(m_point.x + m_size.cx/2, m_point.y + m_size.cy));
	dc->SelectObject(penOld);
	pen.DeleteObject();
	dc->SetBkMode(TRANSPARENT);
	CSize size(m_size.cx/2, m_sizeHeader.cy);
	CPoint pos(m_point.x, m_point.y + (m_size.cy- m_sizeHeader.cy)/2);
	dc->DrawText(m_strName.c_str(), CRect(pos, size), DT_CENTER | DT_WORDBREAK);
}
void CEditItem::Resize(){
	CSize size = CCommonFunction::GetSizeYText(m_strName, m_size.cx/2);
	m_control->SetCx(size.cx - 5);
	m_sizeHeader.SetSize(size.cx, size.cy);
	m_point.x = 0;
	if(m_control && m_control->GetSizeY() > size.cy + 5)
		m_size.cy = m_control->GetSizeY();
	else{
		m_control->SetCy(size.cy + 5);
		m_size.cy = size.cy + 5;
	}
}

void CEditItem::Activate(bool bActive){
	m_control->SetActive(bActive);
//	CEditDraw *pControl = dynamic_cast<CEditDraw*>(m_control);
//	if (NULL != pControl)
//		pControl->SetPosCursor(0);
}

bool CEditItem::ProcessKey(unsigned int nChar, byte stateSysKey){
	return m_control->ProcessKey(nChar, stateSysKey);
}

bool CEditItem::WillActive(){
	return true;
}

std::tstring CEditItem::GetValue(){
	return m_control->GetValue();
}

bool CEditItem::ProcessMouse(unsigned int nButton, CPoint pos, int param){
	if(pos.x > m_point.x + m_size.cx/2)
		return m_control->ProcessMouse(nButton, pos, param);
	return false;
}